﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.individualinfo
{
    public partial class mail_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Session["MemberID"] == null)
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：请重新登录！");
                }

                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty&&ChangeHope.WebPage.PageRequest.GetFormString("id")!="")
                {
                    string types = Request.Form["Option"].Trim();
                    string StrID = ChangeHope.WebPage.PageRequest.GetFormString("id").ToString();
                    if (types == "del")
                    {
                        Del(StrID);
                    }
                    else if (types == "delsend")
                    {
                        DelSend(StrID);
                    }
                    Response.End();
                    return;
                }
               
                if (Cache["state"] == null)
                {
                    Cache["state"] = "1";   
                }
                this.lblList.Text = GetList(Convert.ToInt32(Cache["state"].ToString()));
            }

        }  
        /// <summary>
        /// 收件箱列表 包括收件箱和废件箱
        /// </summary>
        /// <returns></returns>
        protected string GetRecList(string strWhere)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Member.MailReceiver bll = new YXShop.BLL.Member.MailReceiver();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(strWhere);
            //第一步先添加表头
            table.AddHeadCol("", "序号");
            table.AddHeadCol("", "消息主题");
            table.AddHeadCol("", "发件人");
            table.AddHeadCol("", "发送日期");
            table.AddHeadCol("", "已阅");
            table.AddHeadCol("", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(No);
                    string url = YXShop.TemplateAction.Common.UrlStr("shortmessage_detail", "id=" + dataPage.DataReader["id"].ToString(), "membercenter/circlereview/");
                    string title = ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["title"].ToString(), 20, "...");
                    table.AddCol(string.Format("<a href=\'" + url + "\''>{0}</a>", title));
                    table.AddCol(dataPage.DataReader["sender"].ToString());
                    table.AddCol(dataPage.DataReader["receiveTime"].ToString());
                    table.AddCol(Int32.Parse(dataPage.DataReader["isread"].ToString()) == 1 ? "已阅" : "未阅");
                    table.AddCol("<a href='javascript:Del(" + dataPage.DataReader["id"].ToString() + ")'>删除</a>");
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        /// <summary>
        /// 发件箱列表 包括草稿箱和发件箱
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        protected string GetSendList(string strWhere)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Member.Mailinfo bll = new YXShop.BLL.Member.Mailinfo();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(strWhere);
            //第一步先添加表头
            table.AddHeadCol("", "序号");
            table.AddHeadCol("", "消息主题");
            table.AddHeadCol("", "发送日期");
            table.AddHeadCol("", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                while (dataPage.DataReader.Read())
                {
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol(No);
                    string state = dataPage.DataReader["stat"].ToString();
                    string url = string.Empty;
                    if (state == "1") //如果是草稿箱
                    {
                        url = YXShop.TemplateAction.Common.UrlStr("shortmessage_create", "id=" + dataPage.DataReader["id"].ToString(), "membercenter/circlereview/");
                    }
                    else //如果是发件箱
                    {
                        url = "#";
                    }
                    string title = ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["title"].ToString(), 20, "...");
                    table.AddCol(string.Format("<a href=\'" + url + "\''>{0}</a>", title));
                    table.AddCol(dataPage.DataReader["sendtime"].ToString());
                    table.AddCol(string.Format("<a href='javascript:DelSend({0})'>删除</a>", dataPage.DataReader["id"].ToString()));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        /// <summary>
        /// 收件箱删除
        /// </summary>
        /// <param name="id"></param>
        private void Del(string id)
        {
            YXShop.BLL.Member.MailReceiver bll = new YXShop.BLL.Member.MailReceiver();
            YXShop.Model.Member.MailReceiver model = bll.GetModelByID(Convert.ToInt32(id));
            //把收件箱的信件丢入废件箱
            if (model.Stat == 0)
            {
                bll.Amend(Convert.ToInt32(id), "stat", 1);
            }
            //如果本来就在废件箱 那就直接删除
            else if (model.Stat == 1)
            {
                bll.Delete(id);
            }

            Response.Write("ok");
        }

        /// <summary>
        /// 发件箱删除
        /// </summary>
        /// <param name="id"></param>
        private void DelSend(string id)
        {
            YXShop.BLL.Member.Mailinfo bll = new YXShop.BLL.Member.Mailinfo();
            YXShop.Model.Member.MailInfo model = bll.GetModelByID(Convert.ToInt32(id));
            bll.Delete(id);
            Response.Write("ok");
        }

        //收件
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            Cache["state"] = "1";
            this.lblList.Text = GetList(Convert.ToInt32(Cache["state"].ToString()));
        }
        //发件
        protected void btnDeliver_Click(object sender, EventArgs e)
        {
            Cache["state"] = "2";
            this.lblList.Text = GetList(Convert.ToInt32(Cache["state"].ToString()));
        }
        //草稿
        protected void btnDraft_Click(object sender, EventArgs e)
        {
            Cache["state"] = "0";
            this.lblList.Text = GetList(Convert.ToInt32(Cache["state"].ToString()));
        }
        //废稿
        protected void btnWaster_Click(object sender, EventArgs e)
        {
            Cache["state"] = "3";
            this.lblList.Text = GetList(Convert.ToInt32(Cache["state"].ToString()));
        }
        protected string GetList(int state)
        {
            string strList = string.Empty;
            switch (state)
            {
                case 0: //草稿箱
                    strList = GetSendList(" 1=1 and stat=1 and sender='" + Session["MemberName"].ToString() + "'");
                    break;
                case 2: //发件箱
                    strList = GetSendList(" 1=1 and stat=0 and sender='" + Session["MemberName"].ToString() + "'");
                    break;
                case 1: //收件箱
                    strList = GetRecList(" 1=1 and stat=0 and receiverid=" + Session["MemberID"].ToString());
                    break;
                case 3: //废件箱
                    strList = GetRecList(" 1=1 and stat=1 and receiverid=" + Session["MemberID"].ToString());
                    break;
                default:
                    strList = GetRecList(" 1=1 and stat=0 and receiverid=" + Session["MemberID"].ToString());
                    break;
            }
            return strList;
        }
    }
}
